<html>
<head><meta charset="utf-8"><title>Associated error types? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html">Associated error types?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223381943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223381943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223381943">(Jan 20 2021 at 14:49)</a>:</h4>
<p>In a new <code>Write</code>-like trait in a library, what are cases where having an associated type for errors would be useful? As opposed to forcing the use of a single type defined in the library. <a href="https://github.com/japaric/ufmt/issues/27">https://github.com/japaric/ufmt/issues/27</a></p>



<a name="223382234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223382234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223382234">(Jan 20 2021 at 14:51)</a>:</h4>
<p>For formatting traits specifically, I feel like the error types used by <code>Display</code> and <code>Debug</code> don't really add value since they throw away all info about the error</p>



<a name="223382552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223382552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223382552">(Jan 20 2021 at 14:53)</a>:</h4>
<p>To make it more concrete, <a href="https://foss.heptapod.net/octobus/rust/format-bytes/">https://foss.heptapod.net/octobus/rust/format-bytes/</a> formats to arbitrary bytes instead of Unicode. The two options I’m considering is a new trait with an associated error type, or <code>std::io::Write</code> which always uses <code>std::io::Error</code>.</p>



<a name="223382721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223382721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223382721">(Jan 20 2021 at 14:54)</a>:</h4>
<p>Do you want to be <code>#![no_std]</code> friendly? Then, <code>io::Error</code> and <code>io::Write</code> are not an option.</p>



<a name="223385707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223385707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223385707">(Jan 20 2021 at 15:09)</a>:</h4>
<p>Will anything take a fallible closure? e.g.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Thing</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">f</span>: <span class="nc">impl</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;..</span><span class="p">.</span><span class="o">&gt;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="223420291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223420291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223420291">(Jan 20 2021 at 19:11)</a>:</h4>
<p>Good point <span class="user-mention" data-user-id="211727">@Jonas Schievink</span>, fortunately not a concern for us</p>



<a name="223420391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223420391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223420391">(Jan 20 2021 at 19:12)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> No, why?</p>



<a name="223423852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223423852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223423852">(Jan 20 2021 at 19:36)</a>:</h4>
<p>I haven’t thought deeply about the specific case, but one of the painful things is around composing error types when there’s the library error and “my” error.</p>



<a name="223423941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223423941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223423941">(Jan 20 2021 at 19:37)</a>:</h4>
<p>With one true library error, I have to hope that you provided a way to construct your error from my error.</p>



<a name="223424130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Associated%20error%20types%3F/near/223424130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Associated.20error.20types.3F.html#223424130">(Jan 20 2021 at 19:39)</a>:</h4>
<p>When things can be parametrized, then I have a shot at unifying the errors.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>